<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">
            <script>
                function title() {
                    document.getElementById('admin').className='selected';
                }
                window.onload=title;
            </script>
            <h:form id="incomeCreate" enctype="multipart/form-data">
                <h1 class="title">
                    <a href="#" class="back" onclick="history.go(-1)"/>
                    Balanço Financeiro por Procedimento
                </h1>
                <p:messages showDetail="true" autoUpdate="true"  /> 
                <div class="row">
                    <div class="twelve columns">
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Filtros</h4></legend>
                            <p:panel id="resultPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix " >


                                <p:outputLabel for="procedure" value="Procedimento: " styleClass="grid_4"/>  
                                <p:inputText value="#{managerFinancial.procedure.name}"
                                             id="procedure" readonly="true"  styleClass="grid_10" style="margin-right:0px;"/>
                                <p:commandButton  icon="ui-icon-plus" type="button"
                                                  styleClass="ui-button-icon-only" 
                                                  style="width: 25px; height: 28px; margin-left:0px;" 
                                                  onclick="dlgProcedureDialog.show()"/>

                                <div class="clear"/>

                                <p:outputLabel for="de" value="De: " styleClass="grid_4"/>  
                                <p:calendar id="de" value="#{managerFinancial.dateInitial}"
                                            styleClass="grid_4"  showOn="button" pattern="dd/MM/yyyy"/> 

                                <p:outputLabel for="ate" value="Até: " styleClass="grid_2"/>  
                                <p:calendar id="ate" value="#{managerFinancial.dateFinal}" 
                                            styleClass="grid_4"  showOn="button" pattern="dd/MM/yyyy"/> 



                                <div class="clear"/>

                                <div class="buttonAction">
                                    <p:commandButton value="Pesquisar" icon="ui-icon-search" process="@form"
                                                     update="tableEntrada"
                                                     actionListener="#{managerFinancial.searchByProcedure()}" />
                                </div>

                                <p:dataTable id="tableEntrada" var="in" value="#{managerFinancial.incomes}" 
                                             styleClass="grid_24"
                                             rowIndexVar="rowid"
                                             rowKey="#{r.id}"
                                             rows="15" paginator="true"  
                                             paginatorPosition="bottom"
                                             emptyMessage="Nenhum registro"
                                             >  
                                    <f:facet name="header">
                                        Registros (#{managerFinancial.incomes.size()})
                                    </f:facet>

                                    <p:column width="5">
                                        <p:rowToggler />
                                    </p:column>
                                    <p:column headerText="Em" style="white-space: pre-wrap;" width="400">  
                                        <h:outputText value="#{in.createAt}">
                                            <f:convertDateTime pattern="dd/MM/yyy HH:mm:ss" />
                                        </h:outputText>
                                    </p:column>
                                    <p:column headerText="Recebido por" style="white-space: pre-wrap;" width="400">  
                                        <h:outputText value="#{in.systemUser.name}"/>
                                    </p:column>  
                                    <p:column headerText="Procedimento" width="100" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.procedure.name}"/>
                                    </p:column>  
                                    <p:column headerText="Convênio" width="50" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.diary.operator.name}"/>
                                    </p:column>
                                    <p:column headerText="Médico" width="100" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.doctor.name}"/>
                                    </p:column>  
                                    <p:column headerText="Data Realização" width="400" style="white-space: pre-wrap;">  
                                        <h:outputText value="#{in.diary.startTime}">
                                            <f:convertDateTime pattern="dd/MM/yyyy" />
                                        </h:outputText>
                                    </p:column>  
                                    <p:column headerText="Forma" width="400">  
                                        <h:outputText value="#{in.paymentType.name}"/>
                                    </p:column>  
                                    <p:column headerText="Valor Bruto" width="400" style="text-align: right;">  
                                        <h:outputText value="#{in.amount}">
                                            <f:convertNumber pattern="#0.00" />
                                        </h:outputText>
                                        <f:facet name="footer" > 
                                            <h:outputText value="Total:" style="float: right; font-weight:bold;"/>

                                        </f:facet> 
                                    </p:column>  
                                    <p:column rendered="false" headerText="Valor Liquido" width="400" style="text-align: right;">  
                                        <h:outputText value="#{in.totalAmount}"/>
                                        <f:facet name="footer" > 
                                            <h:outputText value="Total:" style="float: right; font-weight:bold;"/>

                                        </f:facet> 
                                    </p:column>  
                                    <p:column headerText="Valor Pago" width="400" style="text-align: right;">  
                                        <h:outputText value="#{in.amountPaid}">
                                            <f:convertNumber pattern="#0.00" />
                                        </h:outputText>
                                        <f:facet name="footer"> 
                                            <h:outputText value="#{managerFinancial.total}" 
                                                          style="float: right; font-weight:bold;">
                                                <f:convertNumber pattern="#0.00" />
                                            </h:outputText>

                                        </f:facet> 
                                    </p:column>
                                    <p:rowExpansion>
                                        <p:panel style="background: none; width: 97%;" 
                                                 styleClass="container_24 clearfix " header="Detalhes" >

                                            <div class="grid_4">
                                                <p:graphicImage  width="100" styleClass="image"  alt="#{in.diary.customer.annex.id}" 
                                                                 title="#{in.diary.customer.annex.id}"
                                                                 url="#{in.diary.customer.annex.id eq null ? '':'show/?annex='}#{in.diary.customer.annex.id eq null ? 'assets/images/avatar.jpg':in.diary.customer.annex.id}" />
                                            </div>
                                            <div class="grid_20">
                                                <p:outputLabel value="Paciente: " styleClass="grid_2"/>
                                                <h:outputText value="#{in.diary.customer.name}" styleClass="grid_10"/>

                                                <p:outputLabel value="Situação: " styleClass="grid_3"/>
                                                <h:outputText value="#{in.diary.diarySituation.toString()}" styleClass="grid_5"/>


                                                <div class="clear"/>

                                                <p:outputLabel value="Observações: " styleClass="grid_5"/>
                                                <h:outputText value="#{in.diary.observation}" styleClass="grid_18"/>

                                                <div class="clear"/>

                                                <p:outputLabel value="Atendimento Realizado em:" styleClass="grid_7"/>
                                                <h:outputText value="#{in.diary.startTime}" styleClass="grid_10">
                                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"/>
                                                </h:outputText>

                                                <div class="clear"/>

                                                <p:outputLabel value="Atendente Responsável:" styleClass="grid_7"/>
                                                <h:outputText value="#{in.diary.systemUser.name}" styleClass="grid_13"/>

                                                <div class="clear"/>

                                                <h:outputText value="Agendamento Realizado em:" styleClass="grid_7"/>
                                                <h:outputText value="#{in.diary.createAt}" styleClass="grid_5">
                                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"/>
                                                </h:outputText>
                                                
                                                <p:button icon="ui-icon-search" outcome="atendimentoprocedimento.xhtml" 
                                                          value="Visualizar Atendimento" styleClass="right ui-priority-primary">
                                                    <f:param name="step" value="3"  />
                                                    <f:param name="diary" value="#{in.diary.id}" />
                                                </p:button>

                                            </div>

                                        </p:panel>
                                    </p:rowExpansion>
                                </p:dataTable> 
                            </p:panel>

                            <p:commandButton 
                                value="Imprimir" icon="ui-icon-print" immediate="true"
                                ajax="false"  onclick="this.form.target='_blank'"
                                styleClass="right" action="#{managerFinancial.reportAll()}"/>
                        </fieldset>
                    </div>
                </div>

                <p:dialog id="dlgDoctor"  widgetVar="dlgProcedureDialog"
                          header="Pesquisar Procedimento" modal="true"
                          width="800"  dynamic="true" > 

                    <p:messages autoUpdate="true" closable="true"/>

                    <p:panel id="medicoPanelSearch" style="background: none; border: none;" 
                             styleClass="container_24 clearfix ">

                        <p:outputLabel value="Nome:" styleClass="grid_3"/>
                        <p:inputText styleClass="grid_20" value="#{managerFinancial.nameProcedure}"/>

                        <div class="buttonAction">
                            <p:commandButton value="Pesquisar" icon="ui-icon-search"
                                             update="medicoPanelSearch" process="medicoPanelSearch"
                                             actionListener="#{managerFinancial.listProcedure()}"
                                             styleClass="ui-priority-primary" />
                        </div >

                        <p:dataTable var="pcd" id="doctorDataTable" value="#{managerFinancial.procedures}"  
                                     style="white-space: pre-wrap;" selectionMode="single" rowKey="#{pcd.id}"
                                     selection="#{managerFinancial.procedure}"
                                     emptyMessage="Nenhum registro encontrado" sortMode="multiple"
                                     paginatorPosition="bottom" paginator="true" rows="5"
                                     >

                            <p:ajax event="rowSelect"  process="@this" update="@this"/>
                            <p:ajax event="rowUnselect"  process="@this" update="@this"/>

                            <f:facet name="header">
                                Procedimentos
                            </f:facet> 
                            <p:ajax event="rowSelect"  process="@this" update="@this"/>
                            <p:ajax event="rowUnselect"  process="@this" update="@this"/>
                            <p:column  headerText="Cód" >  
                                <h:outputText value="#{pcd.cod}" />  
                            </p:column>
                            <p:column  headerText="Nome" >  
                                <h:outputText value="#{pcd.name}" />  
                            </p:column>

                        </p:dataTable>

                        <div class="buttonAction">
                            <p:commandButton value="Selecionar" icon="ui-icon-search"
                                             update="@this, :incomeCreate:resultPanel" 
                                             process="@this, :incomeCreate:resultPanel" 
                                             immediate="false" oncomplete="dlgProcedureDialog.hide();" />

                            <p:commandButton value="Remover" icon="ui-icon-trash"
                                             update="@this, :incomeCreate:resultPanel" 
                                             process="@this, :incomeCreate:resultPanel" 
                                             oncomplete="dlgProcedureDialog.hide();"
                                             immediate="false" actionListener="#{managerFinancial.clearProcedure()}" />
                        </div>
                    </p:panel>
                </p:dialog>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
